﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NHibernateDB;

namespace SmartAnnotation
{
    public partial class SmartAnnotationForm : Form
    {
        private Bitmap                  bmp;
        private Annotation              annotationManager;
        private IList<AdnotariMedicale> admedList;
        private int                     IdImagine;
        private string                  NumeFisier;
        private string                  limba;

        public SmartAnnotationForm(string IdImagine, string NumeFisier, string limba)
        {
            // Init
            InitializeComponent();
            Rename(limba);

            this.IdImagine = Int32.Parse(IdImagine);
            this.NumeFisier = NumeFisier;
            this.limba = limba;

            // Init the image
            bmp = new Bitmap(NumeFisier);
            pictureBox1.Image = bmp;
            annotationManager = new Annotation(pictureBox1, vScrollBar, hScrollBar);

            // Get annotation from DB
            PersistenceManager pm = PersistenceManager.Instance;
            admedList = pm.RetrieveEquals<AdnotariMedicale>("IdImagine", Int32.Parse(IdImagine));

            foreach (AdnotariMedicale admed in admedList)
            {
                annotationManager.AnnotationList.AddLast(new RectangleType(admed.Id, admed.x, admed.y, admed.w, admed.h, admed.Titlu, admed.Descriere));
            }

        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            // Perform a cleanup
            bmp.Dispose();
            bmp = null;
            System.IO.File.Delete(NumeFisier);
            this.Dispose();
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            PersistenceManager pm = PersistenceManager.Instance;

            foreach (RectangleType rect in annotationManager.AnnotationList)
            {
                if (rect.GetID() != 0)
                {
                    foreach (AdnotariMedicale admed in admedList)
                        if (rect.GetID() == admed.Id &&
                            (!rect.GetText().Equals(admed.Descriere) ||
                            !rect.GetTitle().Equals(admed.Titlu) ||
                            rect.GetX() != admed.x ||
                            rect.GetY() != admed.y ||
                            rect.GetWidth() != admed.w ||
                            rect.GetHeight() != admed.h))
                        {
                            pm.Delete(admed);

                            AdnotariMedicale am = new AdnotariMedicale();

                            IList<Login> ListLogin = pm.RetrieveEquals<Login>("Nume", "MedicalDB");

                            am.Titlu = rect.GetTitle();
                            am.IdDoctor = ListLogin[0].Id;
                            am.Descriere = rect.GetText();
                            am.IdImagine = IdImagine;
                            am.x = rect.GetX();
                            am.y = rect.GetY();
                            am.w = rect.GetWidth();
                            am.h = rect.GetHeight();

                            pm.Save(am);
                        }
                }
                else
                {
                    AdnotariMedicale am = new AdnotariMedicale();
                    IList<Login> ListLogin = pm.RetrieveEquals<Login>("Nume", "MedicalDB");

                    am.Titlu = rect.GetTitle();
                    am.IdDoctor = ListLogin[0].Id;
                    am.Descriere = rect.GetText();
                    am.IdImagine = IdImagine;
                    am.x = rect.GetX();
                    am.y = rect.GetY();
                    am.w = rect.GetWidth();
                    am.h = rect.GetHeight();

                    pm.Save(am);
                }
            }

            bool found;
            foreach (AdnotariMedicale admed_orig in admedList)
            {
                found = false;

                foreach (AnnotationType admed_lc in annotationManager.AnnotationList)
                    if (admed_orig.Id == admed_lc.GetID())
                    {
                        found = true;
                        break;
                    }

                if (!found)
                    // => the user deleted the annotation
                    pm.Delete(admed_orig);
            }
        }
    }
}
